/**********************************************************************
* Filename: Campeonato.java
* Created: 2012/04/30
* @author Fernando Brito e Abreu
**********************************************************************/
package org.bunny.businessLayer;

import java.util.Set;
import java.util.HashSet;

public class Campeonato{
	public static Set<Campeonato> allInstances = new HashSet<Campeonato>();
	
	private TipoCampeonato genero;
	private String nome;
	
	/**********************************************************************
	* Default constructor
	**********************************************************************/
	public Campeonato(){
		allInstances.add(this);
	}
	
	/**********************************************************************
	* Parameterized constructor
	* @param genero the genero to initialize
	* @param nome the nome to initialize
	**********************************************************************/
	public Campeonato(TipoCampeonato genero, String nome){
		this.genero = genero;
		this.nome = nome;
		allInstances.add(this);
	}
	/**********************************************************************
	* Standard attribute getter
	* @return the genero of the campeonato
	**********************************************************************/
	public TipoCampeonato genero(){
		return genero;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param genero the genero to set
	**********************************************************************/
	public void setGenero(TipoCampeonato genero){
		this.genero = genero;
	}
	
	/**********************************************************************
	* Standard attribute getter
	* @return the nome of the campeonato
	**********************************************************************/
	public String nome(){
		return nome;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param nome the nome to set
	**********************************************************************/
	public void setNome(String nome){
		this.nome = nome;
	}
	
	/**********************************************************************
	* ONE2MANY getter for Campeonato[1] <-> Grupo[*]
	* @return the grupos of the campeonato
	**********************************************************************/
	public Set<Grupo> grupos(){
		Set<Grupo> result = new HashSet<Grupo>();
		for (Grupo x : Grupo.allInstances)
			if (x.campeonato()  ==  this)
				result.add(x);
		return result;
	}
	
	/**********************************************************************
	* ONE2MANY setter for Campeonato[1] <-> Grupo[*]
	* @param grupos the grupos to set
	**********************************************************************/
	public void setGrupos(Set<Grupo> grupos){
		for (Grupo x : grupos)
			x.setCampeonato(this);
	}
	
	/**********************************************************************
	* MEMBER2ASSOCIATIVE getter for Campeonato[*] <-> Participacao[2..*]
	* @return the participacao of the participado
	**********************************************************************/
	public Set<Participacao> participacao(){
		Set<Participacao> result = new HashSet<Participacao>();
		for (Participacao x : Participacao.allInstances){
			if (x.participado()  ==  this){
				result.add(x);
			}
		}
		return result;
	}
	
	/**********************************************************************
	* MEMBER2ASSOCIATIVE setter for Campeonato[*] <-> Participacao[2..*]
	* @param participacao the participacao to set
	**********************************************************************/
	public void setParticipacao(Set<Participacao> participacao){
		for (Participacao x : participacao){
			x.setParticipado(this);
		}
	}
	
	/**********************************************************************
	* MEMBER2MEMBER getter for Campeonato[*] <-> Pais[2..*]
	* @return the participante of the participado
	**********************************************************************/
	public Set<Pais> participante(){
		Set<Pais> result = new HashSet<Pais>();
		Set<Participacao> lista = Participacao.allInstances;
		int size = lista.size();
		
		
		for (Participacao x : lista){
			Campeonato c=x.participado();
			Pais parti = x.participante();
			
			if (c  ==  this){
				result.add(parti);
			}
		}
		return result;
	}
	
	/**********************************************************************
	* MEMBER2MEMBER setter for Campeonato[*] <-> Pais[2..*]
	* @param participante the participante to set
	**********************************************************************/
	public void setParticipante(Set<Pais> participante){
		for (Pais t : participante){
			Set<Participacao> lista = Participacao.allInstances;
			
			for (Participacao x: lista){
				Campeonato lol = x.participado();
				if (lol == this){
					x.setParticipante(t);
				}
			}
		}
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	**********************************************************************/
//	public void criaMembrosEquipas(){
//		//	TODO
//		//	for eq in self.grupos->collectNested($e : Grupo | $e.jogos)->flatten->collectNested($e : Jogo | $e.equipa)->flatten->asSet do eq.criaFichaJogoFalsa() end;
//	}
	
//	/**********************************************************************
//	* User-defined operation specified in SOIL/OCL
//	* @param nome the nome to set
//	* @param genero the genero to set
//	* @param paises the paises to set
//	* @param grupos the grupos to set
//	**********************************************************************/
//	public void inicializa(String nome, TipoCampeonato genero, Set<String> paises, Set<String> grupos){
//		//	TODO
//		this.nome = nome;
//		this.genero = genero;
//		
//		for (Pais p : Pais.allInstances){
//			Participacao pa = new Participacao();
//		}
//		//	self.nome := nome;
//		//	 self.genero := genero;
//		//	 for p in paises do participacao := new Participacao(((p + '_') + self.nome)) between (new Pais(p),self);
//		//	 participacao.organizador := false;
//		//	 treinador := new Treinador(('Treinador_' + p));
//		//	 insert (treinador,participacao) into Treinador_Participacao end;
//		//	 for g in grupos do insert (self,new Grupo(g)) into Campeonato_Grupo end;
//	}
	
	//	PRE-CONDITIONS (TODO)
	/*
	pre ArgumentosCampeonatoForamDefinidos
		(((nome.isDefined and genero.isDefined) and not paises->isEmpty) and not grupos->isEmpty)
	
	*/
	
	//	POST-CONDITIONS (TODO)
	/*
	post ArgumentosForamIncializados
		(self.nome.isDefined and self.genero.isDefined)
	
	*/
	
	/* (non-Javadoc)
	* @see java.lang.Object#toString()
	*/
	@Override
	/**********************************************************************
	* Object serializer
	**********************************************************************/
	public String toString(){
		return "Campeonato [genero=" + genero + ", nome=" + nome + "]\n";
	}
	
}
